import BaseView from "../Base/BaseView";
import CustomButton from "../Common/CustomButton";
import { EBundleType, EResType, EViewType } from "../Common/Enum";
import EventID from "../Common/EventID";
import { GlobalData } from "../Common/GlobalData";
import Message from "../Common/Message";
import { IGameData, ISettingData, IViewData } from "../Interface/IMessage";
import { AdManager } from "../naodong/ads/AdManager";
import { ELayerType, SceneGame } from "../SceneGame";
import AppService from "../Service/AppService";
import AudioManager from "../Util/AudioManager";
import { LoadUtil } from "../Util/LoadUtil";
import StorageUtil from "../Util/StorageUtil";

const { ccclass, property } = cc._decorator;

const VIEW_DATA: IViewData = {
    name: 'SettingUI',
    type: EViewType.Panel,
    bundle: EBundleType.Game,
    zIndex: 100,
};
@ccclass
export default class SettingUI extends BaseView {

    @property({ type: cc.Node, tooltip: "音乐开关按钮" })
    private btnSound: cc.Node = null;
    @property({ type: cc.Node, tooltip: "震动开关按钮" })
    private btnShake: cc.Node = null;

    @property({ type: cc.Node, tooltip: "icon父节点" })
    private icons: cc.Node = null;
    @property({ type: cc.Node, tooltip: "icon" })
    private icon: cc.Node = null;

    private gameData: IGameData = null;
    private settingData: ISettingData[] = [];

    protected onLoad() {
        super.onLoad();

        this.gameData = StorageUtil.instance.getGameData();
        this.settingData = StorageUtil.instance.getSettingData();

        this.init();
        this.refresh();
        this.setSwitchBtn();
    }

    private init() {
        const config = GlobalData.getSetingIcons();
        for (let i = 0; i < config.length; i++) {
            const icon = cc.instantiate(this.icon);
            icon.parent = this.icons;
            icon.active = true;
            icon.y = 0;
            icon.getComponent(CustomButton).clickEvents[0].customEventData = `${i + 1}`;
        }
    }

    private refresh() {
        this.icons.children.forEach((o, i) => {
            let data = this.settingData[i];
            if (!data) {
                data = { id: i + 1, isSelected: 0, isUnlock: 0 };
            }
            const touxiang = cc.find('touxiang', o).getComponent(cc.Sprite);
            const duigou = cc.find('duigou', o);
            const shipin = cc.find('shipin', o);
            // 显示
            duigou.active = data.isSelected === 1;
            shipin.active = data.isUnlock === 0;
            // 头像
            const config = GlobalData.getSettingIcon(data.id);
            LoadUtil.instance.setSpriteFrame(touxiang, EResType.BubbleIcon, config.name, 90, 90);
        });
    }

    private setIconData(id: number) {
        const index = id - 1;
        for (let i = 0; i < this.settingData.length; i++) {
            const data = this.settingData[i];
            if (index === i && data.isUnlock === 0) {
                // 播放视频
                AdManager.showVideoAd(() => {
                    console.log("视频解锁头像:", id);
                    data.isUnlock = 1;
                    data.isSelected = 1;
                });
                break;
            } else {
                // data.isUnlock = i === index ? 1 : 0;
                data.isSelected = i === index ? 1 : 0;
            }
        }
        this.refresh();
    }

    private setSwitchBtn() {
        const selected1 = cc.find('xuanzhong', this.btnSound);
        const selected2 = cc.find('xuanzhong', this.btnShake);
        const icon1 = cc.find('icon', this.btnSound);
        const icon2 = cc.find('icon', this.btnShake);
        selected1.active = this.gameData.isPlayMusic === 1;
        selected2.active = this.gameData.isShake === 1;
        icon1.x = this.gameData.isPlayMusic === 1 ? 75 : -72;
        icon2.x = this.gameData.isShake === 1 ? 75 : -72;
    }

    private onBtnsClicked(event: cc.Event.EventTouch, data: string) {
        const name = event.target.name;
        switch (name) {
            case 'btnClose':
                {
                    SceneGame.instance.removePopView(this.node);
                }
                break;
            case 'sound':
                {
                    this.gameData.isPlayMusic = this.gameData.isPlayMusic === 1 ? 0 : 1;
                    this.gameData.isPlaySound = this.gameData.isPlaySound === 1 ? 0 : 1;
                    this.setSwitchBtn();
                    AudioManager.instance.setEnabled(Boolean(this.gameData.isPlayMusic));
                }
                break;
            case 'shake':
                {
                    this.gameData.isShake = this.gameData.isShake === 1 ? 0 : 1;
                    this.setSwitchBtn();
                }
                break;
            case 'btnSave':
                {
                    StorageUtil.instance.save();
                }
                break;
            case 'btnReset':
                {
                    StorageUtil.instance.clear();
                    SceneGame.instance.removePopView(this.node);
                    SceneGame.instance.showLayer(ELayerType.LayerLoad);
                    Message.instance.send(EventID.REFRESH_TOP_BAR);
                }
                break;
            case 'btnPrivate':
                {
                    console.log('隐私协议');
                }
                break;
            case 'box':
                {
                    console.log("data:", data);
                    this.setIconData(Number(data));
                }
                break;
        }
    }
}
AppService.instance.registViewData(VIEW_DATA);